package com.mall.shopping.services;

import com.mall.shopping.INavigationService;
import com.mall.shopping.constant.GlobalConstants;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.converter.PanelListToPanelListConverter;
import com.mall.shopping.dal.entitys.PanelContent;
import com.mall.shopping.dal.persistence.PanelContentMapper;
import com.mall.shopping.dto.NavListResponse;
import com.mall.shopping.dto.PanelContentDto;
import com.mall.shopping.utils.ExceptionProcessorUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Slf4j
@Service
@Component
public class INavigationServiceImpl implements INavigationService {

    @Autowired
    PanelContentMapper panelContentMapper;

    @Autowired
    PanelListToPanelListConverter panelListConverter;


    @Override
    public NavListResponse navigation() {
        NavListResponse response = new NavListResponse();
        try {
            Example example = new Example(PanelContent.class);
            example.createCriteria().andEqualTo("panelId", GlobalConstants.HEADER_PANEL_ID);
            List<PanelContent> panelContents = panelContentMapper.selectByExample(example);
            List<PanelContentDto> panelContentDtos =
                    panelListConverter.panelContentsToContentDtos(panelContents);
            response.setPannelContentDtos(panelContentDtos);
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
            log.info("INavigationServiceImpl.navigation effect row :" + panelContents.size());
        } catch (Exception e) {
            ExceptionProcessorUtils.wrapperHandlerException(response,e);
            log.error("INavigationImpl.navigation occur Exception :" + e);
        }
        return response;
    }
}
